package com.chase.framerwork.action;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;

import com.chase.framerwork.common.Globle;
import com.chase.framerwork.entity.User;
import com.chase.framerwork.service.LoginService;
import com.chase.framerwork.service.MenuService;

/**
 * 登陆操作Action
 * 
 * @author Chase
 * 
 */
@ParentPackage("chase-struts")
@Namespace("/")
@Action(value = "loginAction", results = { @Result(name = "success", location = "/framework/index.jsp"),
        @Result(name = "login", location = "/framework/login.jsp"),
		@Result(name = "logout", location = "/framework/login.jsp")
})
@Scope("prototype")
public class LoginAction extends BaseAction
{
	private static final long serialVersionUID = 5989455606615751305L;

	private static final String LOGIN = "login";
	private static final String LOGOUT = "logout";

	private LoginService loginService;
	private MenuService menuService;

	private String username;
	private String password;
	private String remember;

	/**
	 * Enter the login page.
	 * 
	 * Will first check if cookies saved user information, if saved user
	 * information, we will be automatically logged in, or the user manual need
	 * to sign in.
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterLogin() throws Exception
	{
		// 从Cookie中获取序列化的User,进行反序列化
		username = getCookie(Globle.USERNAME_COOKIE);
		password = getCookie(Globle.PASSWORD_COOKIE);

		if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password))
		{
			User user = loginService.loadUser(username, password);
			if (user != null)
			{

				setSession(Globle.USER_SESSION, user);
				Globle.setUser(user);

				addActionMessage("Login success");

				return SUCCESS;
			}
		}
		return LOGIN;
	}

	public String login() throws Exception
	{

		User user = loginService.loadUser(username, password);
		if (user != null)
		{

			setSession(Globle.USER_SESSION, user);
			Globle.setUser(user);

			if (remember != null)
			{
				// TODO 序列化User, Session中保存序列化后的User,而不是用户名和密码,否则安全性极低.
				setCookie(Globle.USERNAME_COOKIE, username, Globle.COOKIE_INVALID_TIME);
				setCookie(Globle.PASSWORD_COOKIE, password, Globle.COOKIE_INVALID_TIME);
			}

			addActionMessage("Login success");

			return SUCCESS;

		}
		else
		{
			addActionError("The username or password error.");
			return LOGIN;
		}
	}

	public String logout()
	{

		removeSession(Globle.USER_SESSION);
		
		removeCookie(Globle.USERNAME_COOKIE);
		removeCookie(Globle.PASSWORD_COOKIE);
		
		return LOGOUT;
	}

	@Resource
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public LoginService getLoginService()
	{
		return loginService;
	}

	public String getRemember()
	{
		return remember;
	}

	public void setRemember(String remember)
	{
		this.remember = remember;
	}

	public MenuService getMenuService()
	{
		return menuService;
	}

	@Resource
	public void setMenuService(MenuService menuService)
	{
		this.menuService = menuService;
	}
}
